package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.management.commons.exception.AddfeedbackException;
import com.management.commons.exception.ExceptionCode.CommentExceptionCode;
import com.management.controller.form.AddfeedbackForm;
import com.management.controller.form.GetcommentByDefectIdForm;
import com.management.controller.info.AccountInfo;
import com.management.controller.info.GetCommentBydefectIdInfo;
import com.management.model.Comment;
import com.management.dao.CommentDao;
import com.management.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {

    @Override
    public Result FindcommentByDefectId(GetcommentByDefectIdForm getcomment) {
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("defect_id",getcomment.getDefectId());
        List<Comment> comments = baseMapper.selectList(queryWrapper);
        List list = CopyUtils.CopyList(comments, GetCommentBydefectIdInfo.class);
        return Result.getInstance().setData(list);
    }

    @Override
    public Result Addfeedback(AddfeedbackForm addfeedbackForm) {
        Comment comment = CopyUtils.CopyOne(addfeedbackForm, Comment.class);
        AccountInfo principal = (AccountInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        comment.setCreateBy(principal.getAccountName());
        int insert = baseMapper.insert(comment);
        if (insert>0) {
            return Result.getInstance().setMessage("添加成功");
        }
        throw new AddfeedbackException(CommentExceptionCode.ADDFEEDBACK_ERROR);
    }
}
